package com.design.proxy.file2;

// 文件代理类
public class FileProxy implements File {
    private String filename;
    private RealFile realFile;

    private boolean isAdminUser;

    public FileProxy(String filename, boolean isAdminUser) {
        this.filename = filename;
        this.isAdminUser = isAdminUser;
    }

    @Override
    public void read() {
        if (realFile == null) {
            // 增加权限控制功能（假设只有 admin 用户具有读取权限）
            if (!isAdminUser()) {
                System.out.println("Access denied. You don't have permission to read this file.");
                return;
            }
            // 创建真实文件对象
            realFile = new RealFile(filename);
        }
        // 增加日志记录功能
        System.out.println("Logging file access: " + filename);
        // 读取文件
        realFile.read();
    }

    public boolean isAdminUser() {
        return isAdminUser;
    }
}
